package com.example.web.servlet;

import com.example.common.Result;
import com.example.model.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/regist")
public class UserRegister extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    private UserService userService = new UserServiceImpl();
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String check = req.getParameter("check");
        HttpSession session = req.getSession();
        String check_server = (String) session.getAttribute("CHECKCODE_SERVER");
        if (StringUtils.isEmpty(check) || !check_server.equalsIgnoreCase(check)) {
            Result result = Result.fail(400, "验证码填写错误");

            String result_str = objectMapper.writeValueAsString(result);
            System.out.println(result_str);
            resp.getWriter().write(result_str);
            return;
        }
        // 获取用户发送的信息 , 判断用户信息是否正确
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String name = req.getParameter("name");
        String telephone = req.getParameter("telephone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");


        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setName(name);
        user.setTelephone(telephone);
        user.setSex(sex);
        user.setBirthday(birthday);

        // 用户信息填写错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(email) || StringUtils.isEmpty(name) || StringUtils.isEmpty(telephone)||
                StringUtils.isEmpty(sex) || StringUtils.isEmpty(birthday) || StringUtils.isEmpty(check)) {
            Result result = Result.fail(400, "请正确填写注册信息");
            String result_str = objectMapper.writeValueAsString(result);
            System.out.println(result_str);
            resp.getWriter().write(result_str);
            return;
        }
        Result result = null;
        boolean flag = userService.registr(user);
        if (!flag) {
            result = Result.fail(400,"注册失败");
        } else {
            result = Result.success("注册成功");
            System.out.println("注册成功");
        }
        String result_str = objectMapper.writeValueAsString(result);
        resp.getWriter().write(result_str);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
